package com.bloveoacom.weichi.presenter;


import com.bloveoacom.exception.ApiException;
import com.bloveoacom.http.HttpResult;
import com.bloveoacom.http.RxBus;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseListSubscriber2;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.AESCipher;
import com.bloveoacom.util.GsonUtil;
import com.bloveoacom.weichi.bean.Comment2Bean;
import com.bloveoacom.weichi.bean.CreateEosWalletBean;
import com.bloveoacom.weichi.bean.PostDetailBean;
import com.bloveoacom.weichi.bean.RewardBean;
import com.bloveoacom.weichi.bean.SimpleBean;
import com.bloveoacom.weichi.contract.PostDetailContract;

import org.reactivestreams.Publisher;

import io.reactivex.Flowable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.bloveoacom.http.HttpMethods.PAGE_START_SIZE;
import static com.bloveoacom.util.AESCipher.KEY;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/8/22
 * desc  : 我的
 */
public class PostDetailPresenter extends PostDetailContract.Presenter {

    @Override
    public void loadFirstData(boolean isRefresh) {
        pageIndex = PAGE_START_SIZE;
        BaseListSubscriber2<Comment2Bean> listBaseListSubscriber2 = mModel.getCommentList(pageIndex, mView.getPageSize(), mView.getNewsId()).compose(RxHelper.getData()).subscribeWith(new BaseListSubscriber2<Comment2Bean>(mView, isRefresh, pageIndex){
            @Override
            public void onNext(Comment2Bean o) {
                super.onNext(o);
                mView.commentList(o);
                pageIndex++;
            }
        });
        addDispose(listBaseListSubscriber2);
    }

    @Override
    public void loadMoreData() {
        BaseListSubscriber2<Comment2Bean> listBaseListSubscriber2 = mModel.getCommentList(pageIndex, mView.getPageSize(), mView.getNewsId()).compose(RxHelper.getData()).subscribeWith(new BaseListSubscriber2<Comment2Bean>(mView, false, pageIndex){
            @Override
            public void onNext(Comment2Bean o) {
                super.onNext(o);
                mView.commentList(o);
                pageIndex++;
            }
        });
        addDispose(listBaseListSubscriber2);
    }

    @Override
    public void getCoinNewsDetail() {
        BaseSubscriber2<PostDetailBean> objectBaseSubscriber2 = mModel.getCoinNewsDetail(mView.getNewsId()).compose(RxHelper.<PostDetailBean>getData()).subscribeWith(new BaseSubscriber2<PostDetailBean>(mView){
            @Override
            public void onNext(PostDetailBean o) {
                super.onNext(o);
                mView.newDetail(o);
            }
        });
        addDispose(objectBaseSubscriber2);
    }

    @Override
    public void comment( String replyCommentId, String replyUserId, String type, String content, String pwd) {

        BaseSubscriber2<RewardBean> objectBaseSubscriber2 =  mModel.findTimeStamp().subscribeOn(Schedulers.io()).flatMap(new Function<HttpResult<Object>, Publisher<HttpResult<RewardBean>>>() {
            @Override
            public Publisher<HttpResult<RewardBean>> apply(HttpResult<Object> objectHttpResult) throws Exception {
                if (objectHttpResult.isOk()) {
                    Object o = objectHttpResult.data;
                    SimpleBean simpleBean = GsonUtil.GsonToBean(GsonUtil.GsonString(o), SimpleBean.class);
                    String s = AESCipher.decryptAES(simpleBean.timeStamp);
                    CreateEosWalletBean transInfoBean = new CreateEosWalletBean(pwd, s);
                    String sign = AESCipher.encryptAES(GsonUtil.GsonString(transInfoBean), KEY);
                    return mModel.comment(mView.getNewsId(),replyCommentId,replyUserId,type,content,sign);
                } else {
                    return Flowable.error(new ApiException(objectHttpResult.code, objectHttpResult.msg));
                }
            }
        }).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<RewardBean>(mView){
            @Override
            public void onNext(RewardBean o) {
                super.onNext(o);
                mView.comment(o);
                fenpeiEmt(o.overType);
            }
        });
        addDispose(objectBaseSubscriber2);
    }


    @Override
    public void userFocus(String targetUserId, int type) {
        BaseSubscriber2<Object> objectBaseSubscriber2 = mModel.userFocus(targetUserId,type).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<Object>(mView){
            @Override
            public void onNext(Object o) {
                super.onNext(o);
                mView.userFocus(o);
            }
        });
        addDispose(objectBaseSubscriber2);
    }

    @Override
    public void goCollection(int type, int isCollect) {
        BaseSubscriber2<Object> objectBaseSubscriber2 = mModel.goCollection(mView.getNewsId(),type,isCollect).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<Object>(mView){
            @Override
            public void onNext(Object o) {
                super.onNext(o);
                mView.goCollection(o);
            }
        });
        addDispose(objectBaseSubscriber2);
    }

    @Override
    public void collect(String objectId, int type, int isCollected) {
        BaseSubscriber2<RewardBean> objectBaseSubscriber2 = mModel.collect(objectId,type,isCollected).compose(RxHelper.<RewardBean>getData()).subscribeWith(new BaseSubscriber2<RewardBean>(mView){
            @Override
            public void onNext(RewardBean o) {
                super.onNext(o);
                mView.collect(o);
            }
        });
        addDispose(objectBaseSubscriber2);
    }

    @Override
    public void fenpeiEmt(String overType) {
        mModel.fenpeiEmt(mView.getNewsId(),overType).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<>());
    }

}
